package com.game.serivce;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;



import com.component.DataServiceUtil;
import com.data.WDataConversion;
import com.data.dao.IConfigDao;
import com.data.model.ConfigRoom;
import com.data.spring.BeanFactory;
import com.game.model.ConfigRoomService;



public class ConfigService {

	private static ConfigService instance;
	public ArrayList<ConfigRoomService> configRoomServices = new ArrayList<ConfigRoomService>();
	public ArrayList<ConfigRoom> configRooms = new ArrayList<ConfigRoom>();
	static Logger logger = Logger.getLogger(ConfigService.class.getName());
	private IConfigDao configDao;
	public static ConfigService getInstance() {
		if (instance == null) {
			instance = new ConfigService();
			//instance.readAllConfig();
			instance.configDao = (IConfigDao) DataServiceUtil.getInstance().getBean("ConfigDao");
		}
		return instance;
	}

	public ArrayList<ConfigRoomService> getConfigRoomServices() {
		return configRoomServices;
	}

	public void setConfigRoomServices(ArrayList<ConfigRoomService> configRoomServices) {
		this.configRoomServices = configRoomServices;
	}

	public ArrayList<ConfigRoom> getConfigRooms() {
		return configRooms;
	}

	public void setConfigRooms(ArrayList<ConfigRoom> configRooms) {
		this.configRooms = configRooms;
	}

	public void readAllConfig_noBD() {
		try {
			if (configRooms != null) {
				for (int i = 0; i < 1; i++) {
					ConfigRoomService cc = new ConfigRoomService();
					configRoomServices.add(cc);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("commonExceotion", e);

		}
	}

	public void readAllConfig() {
		try {
			List<ConfigRoom> cr = BeanFactory.PlayerService.getAllRoomInfoForConfig();
			if (configRooms != null) {
				for (int i = 0; i < cr.size(); i++) {
					ConfigRoom configRoom = cr.get(i);
					configRooms.add(configRoom);
					ConfigRoomService cc = new ConfigRoomService();
					configRoomServices.add(cc);
					setConfig_gonggongcanshu(i);
					setConfig_zhuyaocanshu(i);
					setConfig_fenshukongzhi(i);
					setConfig_broadcastsetting(i);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("commonExceotion", e);

		}
	}

	public void reRead() {
		try {
			List<ConfigRoom> cr = BeanFactory.PlayerService.getAllRoomInfoForConfig();
			if (configRooms != null) {
				for (int i = 0; i < cr.size(); i++) {
					ConfigRoom configRoom = cr.get(i);
					configRooms.set(i, configRoom);

					setConfig_gonggongcanshu(i);
					setConfig_zhuyaocanshu(i);
					setConfig_fenshukongzhi(i);
					setConfig_broadcastsetting(i);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("commonExceotion", e);

		}
	}

	public void setConfig_gonggongcanshu(int i) {
		try {
			configRoomServices.get(i).isOpen = Integer.valueOf(configRooms.get(i).getIsOpen());
			configRoomServices.get(i).openTime = configRooms.get(i).getOpenTime();
			configRoomServices.get(i).roomType = Integer.valueOf(configRooms.get(i).getRoomType());
			configRoomServices.get(i).roomName = configRooms.get(i).getRoomName();
			configRoomServices.get(i).oneRoomhaveDeskCount = Integer.valueOf(configRooms.get(i).getOneRoomhaveDeskCount());
			configRoomServices.get(i).roomNumberForDb = Integer.valueOf(configRooms.get(i).getId().getRoomId());
			configRoomServices.get(i).gameNumberForDb = Integer.valueOf(configRooms.get(i).getId().getGameId());
			configRoomServices.get(i).bugEmail = configRooms.get(i).getBugEmail().replace('@', '$');
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("commonExceotion", e);

		}
	}

	public String getConfig_gonggongcanshu(int index) {
		try {

			String messageStr = getMessage("isOpen", "是否开启该房间 0-关 1开", "1", configRooms.get(index).getIsOpen() + "", false);
			messageStr += getMessage("openTime", "房间开放时间", "1", configRooms.get(index).getOpenTime() + "", false);
			messageStr += getMessage("roomType", "该房间类型 (经典-0)", "0", configRooms.get(index).getRoomType() + "", false);
			messageStr += getMessage("roomName", "该房间名字", "1", configRooms.get(index).getRoomName() + "", false);
			messageStr += getMessage("oneRoomhaveDeskCount", "该房间有多少张桌子", "1", configRooms.get(index).getOneRoomhaveDeskCount() + "", false);
			messageStr += getMessage("oneDeskhavePlayerCount", "每张桌子上有多少个位置", "1", 1 + "", false);
			messageStr += getMessage("roomNumberForDb", "数据库存贮的房间编号", "0", configRooms.get(index).getId().getRoomId() + "", false);
			messageStr += getMessage("gameNumberForDb", "数据库存贮的游戏编号", "0", configRooms.get(index).getId().getGameId() + "", false);
			messageStr += getMessage("bugEmail", "收到错误邮件邮箱地址", "1", configRooms.get(index).getBugEmail().replace('@', '$') + "", true);
			return messageStr;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("commonExceotion", e);
			return "";
		}
	}

	public void setConfig_zhuyaocanshu(int i) {
		try {
			configRoomServices.get(i).roomLimitMoney = configRooms.get(i).getRoomLimitMoney();
			configRoomServices.get(i).roomLimitTime = configRooms.get(i).getRoomLimitTime();
			configRoomServices.get(i).configId = configRooms.get(i).getConfigId();
			configRoomServices.get(i).betArea = configRooms.get(i).getBetArea();
			//configRoomServices.get(i).autoBetNumArea = configRooms.get(i).getAutoBetNumArea();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("commonExceotion", e);

		}
	}

	public String getConfig_zhuyaocanshu(int index) {
		try {
			String messageStr = getMessage("roomLimitMoney", "进入房间所需要携带的最小金币额", "1", configRooms.get(index).getRoomLimitMoney() + "", false);
			messageStr += getMessage("roomLimitTime", "玩家操作时间限制（单位秒）", "1", configRooms.get(index).getRoomLimitTime() + "", false);
			messageStr += getMessage("configId", "配置文件ID", "1", configRooms.get(index).getConfigId() + "", false);
			messageStr += getMessage("betArea", "押注区间", "1", configRooms.get(index).getBetArea() + "", false);
			messageStr += getMessage("autoBetNumArea", "自动旋转区间", "1", configRooms.get(index).getAutoBetNumArea() + "", true);
			// 公共参数
			return messageStr;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("commonExceotion", e);
		}
		return "";
	}

	public void setConfig_fenshukongzhi(int i) {
		try {
			configRoomServices.get(i).chouShuiChiRate = Float.valueOf(configRooms.get(i).getChouShuiChiRate());
			// 正常返分池
			configRoomServices.get(i).normalFanFenRate = Float.valueOf(configRooms.get(i).getNormalFanFenRate());
			configRoomServices.get(i).normalRandomSection = configRooms.get(i).getNormalRandomSection();
			configRoomServices.get(i).normalPeiRate = Float.valueOf(configRooms.get(i).getNormalPeiRate());
			configRoomServices.get(i).normalBei = configRooms.get(i).getNormalBei();

			configRoomServices.get(i).randomRate = configRooms.get(i).getRandomRate();
			configRoomServices.get(i).totalRate = configRooms.get(i).getTotalRate();

			// configRoomServices.get(i).bonusFanFenRate =
			// Float.valueOf(configRooms.get(i).getBonusFanFenRate());

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("commonExceotion", e);

		}
	}

	public String getConfig_fenshukongzhi(int index) {
		try {
			String messageStr = getMessage("chouShuiChiRate", "普通抽水比例", "1", configRooms.get(index).getChouShuiChiRate() + "", false);

			messageStr += getMessage("normalFanFenRate", "正常返分抽水比例", "1", configRooms.get(index).getNormalFanFenRate() + "", false);
			messageStr += getMessage("normalRandomSection", "正常返分池大于此设定值,不做开奖控制", "1", configRooms.get(index).getNormalRandomSection() + "", false);
			messageStr += getMessage("normalPeiRate", "当返分池分数小于normalRandomSection时，降低开奖概率", "1", configRooms.get(index).getNormalPeiRate() + "", false);
			messageStr += getMessage("randomRate", "生成牌的概率(A-7,K-7,O-5,J-5,钥匙-10,罐子-15,金字塔-15,法老-25,狗头-40,艳后-50,黑猫-BONUS,木乃伊-WILD,眼睛-SCATTER", "1",
					configRooms.get(index).getRandomRate() + "", false);
			messageStr += getMessage("totalRate", "生产牌总数值", "1", configRooms.get(index).getTotalRate() + "", false);
			// bonus控制
			messageStr += getMessage("normalBei", "控制基准倍数", "1", configRooms.get(index).getNormalBei() + "", true);
			// 公共参数
			return messageStr;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("commonExceotion", e);
		}
		return "";
	}

	public void setConfig_broadcastsetting(int i) {
		try {
			configRoomServices.get(i).beiBroadcast = configRooms.get(i).getBeiBroadcast();
			configRoomServices.get(i).contentBroadcast = configRooms.get(i).getContentBroadcast();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("commonExceotion", e);

		}
	}

	public String getConfig_broadcastsetting(int index) {
		try {
			String messageStr = getMessage("beiBroadcast", "广播触发倍数", "1", configRooms.get(index).getBeiBroadcast() + "", false);
			messageStr += getMessage("contentBroadcast", "广播内容(?代表参数)", "1", configRooms.get(index).getContentBroadcast() + "", true);
			// 公共参数
			return messageStr;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("commonExceotion", e);
		}
		return "";
	}

	public String getMessage(String propName, String propDes, String isUpdate, String value, boolean isLast) {
		String message = propName + ";" + propDes + ";" + isUpdate + ";" + value;
		if (!isLast) {
			message += "*";
		}
		return message;
	}
	public void updateConfigRoomForDB(com.data.model.ConfigRoom configRoom){
		configDao.updateConfigRoom(configRoom);
	}
}
